package com.CoreryBlack.controller;

import com.CoreryBlack.po.*;
import com.CoreryBlack.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * @Description ClazzController
 * @Author CoreryBlack
 * @Date 2024-09-19
 */
@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;


    @GetMapping
    public Result list(ClazzQueryParam clazzQueryParam){
        PageBean pageBean = clazzService.list(clazzQueryParam);
        return Result.success(pageBean);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) throws IllegalAccessException {
        try {
            clazzService.deleteById(id);
            return Result.success();
        } catch (IllegalStateException e) {
            log.error("Delete class error: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    @PostMapping
    public Result insert(@RequestBody Clazz clazz){
        clazzService.saveClazz(clazz);
        return Result.success();
    }

    @PutMapping
    public Result updateById(@RequestBody Clazz clazz){
        clazzService.updateClazz(clazz);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("根据id查询员工的详细信息");
        Clazz clazz = clazzService.getInfo(id);
        return Result.success(clazz);
    }

    @GetMapping("/clazzs/queryById/{id}")
    public Result queryById(@PathVariable Integer id){
        Clazz clazz = clazzService.queryById(id);
        return Result.success(clazz);
    }
    @GetMapping("/list")
    public Result list() {
        try {
            // 查询所有班级信息
            List<Clazz> clazzList = clazzService.queryString();
            return Result.success(clazzList); // 返回成功结果
        } catch (Exception e) {
            return null;// 返回失败结果
        }
    }


}
